﻿using System;
using System.IO;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class ProductTest
{
    private StringBuilder result;

    private void target_PriceIncreased(object sender, PriceIncreasementEventArgs args)
    {
        Console.WriteLine("Price was increased from {0} to {1}.", args.OldPrice, args.NewPrice);
    }

    [TestInitialize]
    public void SetConsole()
    {
        result = new StringBuilder();
        StringWriter sr = new StringWriter(result);
        Console.SetOut(sr);
    }

    [TestMethod]
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
    public void ProductConstructorNegativePrice()
    {
        Product p = new Product("Sample", -2);
    }

    [TestMethod]
    [ExpectedException(typeof(ArgumentNullException))]
    public void ProductConstructorNullName()
    {
        Product t = new Product(null, 1);
    }

    [TestMethod]
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
    public void ProductConstructorEmptyName()
    {
        Product t = new Product("", 1);
    }

    [TestMethod]
    public void ProductEventTest()
    {
        string name = "Vafla Morena";
        decimal price = 0.5M;
        Product target = new Product(name, price);
        target.PriceIncreased += target_PriceIncreased;
        string expected = string.Format("Price was increased from {0} to {1}.", 0.5, 1) + Environment.NewLine;
        target.Price = 1;
        Assert.AreEqual(expected, result.ToString());
        Assert.AreEqual(1, target.Price);
    }

    [TestMethod]
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
    public void ProductSetNegativePrice()
    {
        Product p = new Product("Sample", 1);
        p.Price = -1;
    }

    [TestMethod]
    public void ProductNullEvent()
    {
        Product target = new Product("Orbit", 0.8M);
        target.PriceIncreased += target_PriceIncreased;
        target.Price = 0.4M;
        string actual = result.ToString();
        Assert.AreEqual(string.Empty, actual);
    }
}
